package br.hm.ocr.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import br.hm.ocr.OcrException;
import br.hm.ocr.OcrService;

public class HtmlOcrService implements OcrService {
	
	private static final String URL_OCR_DEFAULT = "http://jimbocho.ocrgrid.org/cgi-bin/weocr/submit_tesseract.cgi";
	//private static final String URL_OCR_DEFAULT = "http://jimbocho.ocrgrid.org/cgi-bin/weocr/submit_ocrad.cgi";

	private HttpClient httpClient;

	public HtmlOcrService() {
		this(new DefaultHttpClient());
	}

	public HtmlOcrService(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public String convertImageToText(File file) throws OcrException {
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			return convertImageToText(fileInputStream, file.length());
		} catch (Exception e) {
			throw new OcrException(e.getMessage(), e);
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {}
			}
		}
	}

	@Override
	public String convertImageToText(final InputStream inputStream,
			final long length) throws OcrException {
		LineNumberReader reader = null;
		try {
			HttpPost httppost = new HttpPost(URL_OCR_DEFAULT);

			InputStreamBody userfile = new InputStreamBody(inputStream,
					"teste.jpg") {
				@Override
				public long getContentLength() {
					return length;
				}
			};

			StringBody outputencoding = new StringBody("utf-8");

			MultipartEntity reqEntity = new MultipartEntity(
					HttpMultipartMode.STRICT);
			reqEntity.addPart("userfile", userfile);
			reqEntity.addPart("outputencoding", outputencoding);

			httppost.setEntity(reqEntity);

			HttpResponse response = httpClient.execute(httppost);
			HttpEntity resEntity = response.getEntity();

			reader = new LineNumberReader(new InputStreamReader(
					resEntity.getContent()));
			String tmp = null;
			StringBuilder result = new StringBuilder();
			boolean bs = false;
			while ((tmp = reader.readLine()) != null) {
				if ("</pre>".equals(tmp)) {
					bs = false;
				}
				if (bs) {
					result.append(tmp);
				}
				if ("<pre>".equals(tmp)) {
					bs = true;
				}
			}
			resEntity.consumeContent();
			return result.toString();

		} catch (Exception e) {
			throw new OcrException(e.getMessage(), e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
